<?php
require_once( 'Zend/Db.php' );
require_once( 'Zend/Db/Table.php' );
require_once( 'Zend/Db/Table/Abstract.php' );

class CONSULTA extends Zend_Db_Table_Abstract
{
	protected $_name = 'CONSULTA';
	
	protected $_sequence = true;

	public function turnolibre() {
        $select = $this->select();
        $select=$this->select()->setIntegrityCheck(false);
        
        $select->from($this->_name,'COUNT(*) AS num' )
        		->where('CONSULTA.COFECHA_Y_HORA = ?',$this->fechayhora)
        		->join('REVISA', 'REVISA.COD_CONSULTA = CONSULTA.COD_CONSULTA',array())
        		->where('REVISA.ES__PDNI = ?',$this->dni);
        
        $result = $this->fetchRow($select)->num;
        if ($result == 0)
        {	
        return 1;
        }
        return 0;
    }
    
	public function insConsulta(){
        $data = array('COFECHA_Y_HORA' => $this->fechayhora,
                      'OBSERVACIONES' => $this->obs);
           
        	try{
        	$result = $this->insert($data);

        	}catch (Exception $e){
        		return false;
        	}
            return true;
    }
        public function listConsultas(){
        	$select = $this->select();
        	$select = $this->select()->setIntegrityCheck(false);
        	$parray = array('MED_DNI'=>'REVISA.ES__PDNI', 'PAC_DNI'=>'REVISA.PDNI', 'FECHA'=>'CONSULTA.COFECHA_Y_HORA', 'OBS'=>'CONSULTA.OBSERVACIONES');
       		$select->from($this->_name)
        		->join('REVISA', 'REVISA.COD_CONSULTA = CONSULTA.COD_CONSULTA',$parray)
        		->where('PDNI = ?',$this->dni);
        		
        $result = $this->fetchAll($select);
        return $result;
        }
        
		public function listallConsultas(){
        	$select = $this->select();
        	$select = $this->select()->setIntegrityCheck(false);
        	$parray = array('MED_DNI'=>'REVISA.ES__PDNI', 'PAC_DNI'=>'REVISA.PDNI', 'FECHA'=>'CONSULTA.COFECHA_Y_HORA', 'OBS'=>'CONSULTA.OBSERVACIONES');
       		$select->from($this->_name)
       		->join('REVISA', 'REVISA.COD_CONSULTA = CONSULTA.COD_CONSULTA',$parray);
        		
        		
        $result = $this->fetchAll($select);
        return $result;
        }

}

?>